package com.example.demo.model;

import com.example.demo.enums.ResultCodeEnum;
import lombok.Data;

//统一结果
@Data
public class Result<T> {
    private int code;
    private String errMsg; //业务处理失败时,返回的错误信息
    private T data; //业务返回数据

    public static <T> Result success(T data){
        Result result = new Result();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setData(data);
        return result;
    }

    public static <T> Result fail(String errMsg){
        Result result = new Result();
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result fail(String errMsg, T data){
        Result result = new Result();
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setErrMsg(errMsg);
        result.setData(data);
        return result;
    }
    public static <T> Result fail(int code, String errMsg){
        Result result = new Result();
        result.setCode(code);
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result fail(int code, String errMsg, T data){
        Result result = new Result();
        result.setCode(code);
        result.setErrMsg(errMsg);
        result.setData(data);
        return result;
    }

    public static <T> Result nologin(){
        Result result = new Result();
        result.setCode(ResultCodeEnum.NO_LOGIN.getCode());
        result.setErrMsg("未登录");
        return result;
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getData(){
        return data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public int getCode() {
        return code;
    }
}